class AdminController < ApplicationController

  before_filter :authenticate_user!
  
  ######  Home Page  ######   
  def home   

    @banners = Banners.first    
    if params[:banners] 
      respond_to do |format|
        if @banners.update_attributes(params[:banners])
          format.html { redirect_to "/admin", :notice => 'Post was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render :action => "home", :layout => 'admin' }
          format.json { render :json => @banners.errors, :status => :unprocessable_entity }
        end
      end       
    else           
      respond_to do |format|
        format.html { render :layout => 'admin'} # new.html.erb
        format.json { render :json => @banners}
      end
    end     

  end  
  
  ######  News  ######    
  def news
    @news = News.order("date_from DESC").find(:all)      
    respond_to do |format|
      format.html { render :layout => 'admin'} # new.html.erb
      format.json { render :json => @news}
    end  
  end 
  def news_edit    
    @news = News.find(params[:id])    
    if params[:news]      
      respond_to do |format|
        if @news.update_attributes(params[:news])
          format.html { redirect_to "/admin/news", :notice => 'Post was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render :action => "news_edit", :layout => 'admin' }
          format.json { render :json => @news.errors, :status => :unprocessable_entity }
        end
      end       
    else           
      respond_to do |format|
        format.html { render :layout => 'admin'} # new.html.erb
        format.json { render :json => @news}
      end
    end     
  end     
  def news_delete
    @news = News.find(params[:id])
    @news.destroy
    respond_to do |format|
      format.html { redirect_to "/admin/news" }
      format.json { head :no_content }
    end
  end     
  def news_new
      if params[:news]
        @news = News.new(params[:news])                
        respond_to do |format|
          if @news.save
            format.html { redirect_to "/admin/news", :notice => 'Post was successfully created.' }
            format.json { render :json => @news, :status => :created, :location => "/admin/news" }
          else
            format.html { render :action => "news_new", :layout => 'admin'}
            format.json { render :json => @news.errors, :status => :unprocessable_entity }
          end
        end     
      else       
        @news = News.new
        respond_to do |format|
          format.html { render :layout => 'admin'}
          format.json { render :json => @news}
        end  
      end      
  end  
  
  
  
  ######  lookbook  ######    
  def lookbook
    @lookbook = Lookbook.order("sort DESC").find(:all)      
    respond_to do |format|
      format.html { render :layout => 'admin'} # new.html.erb
      format.json { render :json => @lookbook}
    end  
  end 
  def lookbook_edit    
    @lookbook = Lookbook.find(params[:id])    
    if params[:lookbook]      
      respond_to do |format|
        if @lookbook.update_attributes(params[:lookbook])
          format.html { redirect_to "/admin/lookbook", :notice => 'Post was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render :action => "lookbook_edit", :layout => 'admin' }
          format.json { render :json => @lookbook.errors, :status => :unprocessable_entity }
        end
      end       
    else           
      respond_to do |format|
        format.html { render :layout => 'admin'} 
        format.json { render :json => @lookbook}
      end
    end     
  end     
  def lookbook_delete
    @lookbook = Lookbook.find(params[:id])
    @lookbook.destroy
    respond_to do |format|
      format.html { redirect_to "/admin/lookbook" }
      format.json { head :no_content }
    end
  end     
  def lookbook_new
      if params[:lookbook]
        @lookbook = Lookbook.new(params[:lookbook])                
        respond_to do |format|
          if @lookbook.save
            format.html { redirect_to "/admin/lookbook", :notice => 'Post was successfully created.' }
            format.json { render :json => @lookbook, :status => :created, :location => "/admin/lookbook" }
          else
            format.html { render :action => "lookbook_new", :layout => 'admin'}
            format.json { render :json => @lookbook.errors, :status => :unprocessable_entity }
          end
        end     
      else       
        @lookbook = Lookbook.new
        respond_to do |format|
          format.html { render :layout => 'admin'}
          format.json { render :json => @lookbook}
        end  
      end      
  end 
  
  ### Feedback ###
  
  def feedback         
      @feedback = Feedback.order("id DESC").all 
  end
  def feedback_delete
    @feedback = Feedback.find(params[:id])
    @feedback.destroy
    respond_to do |format|
      format.html { redirect_to "/admin/feedback" }
      format.json { head :no_content }
    end
  end      
  
end
